home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16129 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  72 lines

  1. Path: symiserver2.symantec.com!elazzar1
  2. From: elazzaro@symantec.com (Erin Lazzaro)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Did I Miss Something?
  5. Date: Tue, 09 Apr 96 18:58:06 GMT
  6. Organization: Symantec Corp.
  7. Message-ID: <4ke8p7$i8g@symiserver2.symantec.com>
  8. References: <N.040396.105136.51@ix.netcom.com>
  9.  <4k01o7$sl2@grimsel.zurich.ibm.com> <N.040496.100620.06@ix.netcom.com>
  10. NNTP-Posting-Host: 155.64.65.78
  11. X-Newsreader: News Xpress Version 1.0 Beta #4
  12.  
  13. In article <N.040496.100620.06@ix.netcom.com>,
  14.    jhewett@ix.netcom.com (Jerry Hewett) wrote:
  15. >So if my theory that the act of calling a constructor allocates the necessary 
  16. >memory (like strdup automagically calls malloc to create space in the heap) 
  17. is 
  18. >in error, then the original example (line_of_text = input_line) indeed does 
  19. not 
  20. >carve out a chuck of RAM big enough to hold "small box"?  So even though this 
  21. >works when I compile it, in actuality it is undefined behavior?
  22.  
  23. You're making this way too hard.  Step back and take a breath.  This is _not_
  24. new behavior in C++ -- it's the way C has always done it.
  25.  
  26. Take the example (pure C)
  27.      char * p;
  28.      p = "This is a string constant";
  29.  
  30. "This is a string constant" is a string constant.  In most implementations,
  31. this means that the compiler allocates space for it in a "string table",
  32. which is in initialized global storage.  At the point of reference, it
  33. replaces it with the address in the string table.  So the assignment
  34. compiles to
  35.     p = 0xXXXXX;    /* some address in the string table */
  36. p hasn't been redefined in any way; it's still just a pointer.
  37.  
  38. Don't confuse this "string table" with a Windows stringtable; this is an
  39. internal data structure within the compiler.  All you know is that you are
  40. pointing at a string constant (which may be implemented some other way).
  41.  
  42. A statement like
  43.     char * p = "This is a string constant";
  44. does exactly the same thing.  p isn't const; it is currently pointing to
  45. memory which is const, but it can be reassigned.
  46.  
  47. A constructor like this
  48.     box::box(char * inputline)
  49.     {
  50.        m_inputline = inputline;    // initialize a member of type char*
  51.     }
  52. does not allocate memory.  It causes m_inputline to point to the same
  53. (previously allocated) memory as inputline.
  54.  
  55. Here's some bad code (I use malloc instead of new to make it clearer to C 
  56. readers):
  57.     p = malloc(30);
  58.     strcpy( p, "This is a malloc'd string");
  59.     box1 = new box(p);
  60.     free(p);    // box1->m_inputline now points to free'd memory
  61.  
  62. A declaration like
  63.     box    box1("This is a constant string");
  64. allocates a box whose m_inputline points to a string constant.
  65. m_inputline is not const; it can be reassigned later.  It points
  66. (currently) into const memory.
  67.  
  68. I think you have been misled by unfamiliar terms to think that you are
  69. dealing with unfamiliar concepts.  I hope this is clearer.
  70.  
  71. -Erin Lazzaro
  72.